<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单手机" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择支付状态" clearable
                   size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物流类型" prop="logisticsType">
        <el-select v-model="queryParams.logisticsType" placeholder="请选择物流类型" clearable
                   size="small">
          <el-option
            v-for="dict in logisticsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-row>
              <el-col :span="14">
                <el-row>
                  <el-col :span="24">
                    <el-form-item label="详细地址">
                      <span>{{props.row.province}} {{props.row.city}} {{props.row.address}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="联系电话">
                      {{props.row.mobile}}
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="收件人姓名">
                      {{props.row.truename}}
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="物流类型">
                      {{logisticsTypeFormat(props.row)}}
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="物流号">
                      {{props.row.logisticsNumber}}
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="23">
                    <el-form-item label="备注">
                      {{props.row.remark}}
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-col>
              <el-col :span="10">
                <el-row>
                  <el-col :span="24" class="goods-col-layout">
                    <el-form-item label="订单商品">
                      <div class="goods-layout">
                        <div class="item-layout" v-for="item in props.row.orderGoods">
                          <el-image class="image" :src="item.imageUrl" :preview-src-list="[item.imageUrl]"></el-image>
                          <div class="info-layout">
                            <span class="name">{{item.goodsName}}</span>
                            <span class="spec">{{item.specName}}</span>
                            <div class="info">
                              <span class="money">￥{{item.payMoney}}</span>
                              <span class="total">×{{item.buyTotal}}</span>
                            </div>
                          </div>
                        </div>
                      </div>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderSn"/>
      <el-table-column label="支付交易订单号" align="center" prop="transactionSn"/>
      <el-table-column label="支付方式" align="center" prop="payType"
                       :formatter="payTypeFormat"/>
      <el-table-column label="订单金额" align="center" prop="money"/>
      <el-table-column label="实际支付金额" align="center" prop="practicalMoney"/>
      <el-table-column label="支付状态" align="center" prop="status"
                       :formatter="statusFormat"/>
      <el-table-column label="下单时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="sendOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendOutTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-promotion" @click="updateOrderMoney(scope.row)"
                     v-hasPermi="['order:order:edit']" v-if="scope.row.status == '0'">
            修改支付金额
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-s-promotion" @click="handleUpdate(scope.row)"
                     v-hasPermi="['order:order:edit']" v-if="scope.row.status == '2'">
            发货
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['order:order:edit']" v-if="scope.row.status == '3'">
            更新物流
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"/>

    <add-or-update v-show="open" @on-close="open=false" ref="addOrUpdate"></add-or-update>
  </div>
</template>

<script>
  import {
    listOrder,
    getOrder,
    delOrder,
    updateOrderMoney,
    exportOrder
  } from '@/api/order/order'
  import mixinViewModule from '@/mixins/view-module'
  import AddOrUpdate from './add-or-update'

  export default {
    name: 'Order',
    mixins: [mixinViewModule],
    components: { AddOrUpdate },
    data() {
      return {
        mixinViewModuleOptions: {
          getDetailsURL: getOrder,// 查询详情实体
          getDataListURL: listOrder, // 数据列表接口，API地址
          deleteURL: delOrder, // 删除接口，API地址
          tableKey: 'id', // 删除接口，批量状态下由那个key进行标记操作？比如：pid，uid...
          exportURL: exportOrder // 导出接口，API地址
        },
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 支付方式字典
        payTypeOptions: [],
        // 支付状态;0=待支付,1=已取消,2=已支付,3=待发货,4=已发货,5=已送达字典
        statusOptions: [],
        // 物流类型;SF=顺丰字典
        logisticsTypeOptions: [],
        inputPracticalMoney: 0,
        // 查询参数
        queryParams: {
          uid: null, orderSn: null, status: null, logisticsType: null
          , orderByColumn: 't.create_time'
          , isAsc: 'desc'
        }
      }

    },
    created() {
      this.getList()
      this.getDicts('pay_type').then(response => {
        this.payTypeOptions = response.data
        this.$refs.addOrUpdate.payTypeOptions = response.data
      })
      this.getDicts('order_status').then(response => {
        this.statusOptions = response.data
        this.$refs.addOrUpdate.statusOptions = response.data
      })
      this.getDicts('logistics_type').then(response => {
        this.logisticsTypeOptions = response.data
        this.$refs.addOrUpdate.logisticsTypeOptions = response.data
      })
    },
    methods: {
      // 支付方式字典翻译
      payTypeFormat(row, column) {
        return this.selectDictLabel(this.payTypeOptions, row.payType)
      },
      // 支付状态;0=待支付,1=已取消,2=已支付,3=待发货,4=已发货,5=已送达字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status)
      },
      // 物流类型;SF=顺丰字典翻译
      logisticsTypeFormat(row, column) {
        return this.selectDictLabel(this.logisticsTypeOptions, row.logisticsType)
      },
      //打开dislog前执行
      onOpenHandleDialog() {

      },
      //打开删除前执行
      onHandleDetails(data, type) {
        if (type == this.$formType.edit) {
          this.$refs.addOrUpdate.form = data.data
        }
      },
      updateOrderMoney(item) {
        this.inputPracticalMoney = item.practicalMoney
        const h = this.$createElement
        //message: '<el-input-number v-model="selectOrderInfo" :min="0.01" :max="9999999" label="请输入订单支付金额"></el-input-number>',
        this.$msgbox({
          title: '请输入订单支付金额',
          message: h('div', null, [
            h('spen', null, '当前订单支付金额:' + this.inputPracticalMoney + '元'),
            h('br'),
            h('el-input-number', {
              attrs: {
                value: this.inputPracticalMoney,
                precision: 2,
                controls: false,
                label: '请输入订单支付金额'
              },
              on: {
                input: function(event) {
                  this.inputPracticalMoney = event
                  console.log(this.inputPracticalMoney)
                }.bind(this)
              }
            }),
            h('spen', null, '元')
          ]),
          showCancelButton: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
              instance.confirmButtonLoading = true
              instance.confirmButtonText = '执行中...'
              updateOrderMoney({ id: item.id, money: this.inputPracticalMoney }).then(res => {
                done()
                this.msgSuccess('操作成功')
                this.getList()
              }).catch(() => {
                instance.confirmButtonText = '确定'
              }).finally(error => {
                instance.confirmButtonLoading = false
              })
            } else {
              done()
            }
          }
        })
      },
      //开始查询列表的时候
      onStartQuery() {

      },
      //开始重制查询前执行
      onResetQuery() {

      }
    }
  }
</script>
<style lang="scss">
  .goods-col-layout {
    .el-form-item {
      width: 100%;
    }

    .el-form-item__content {
      width: 100%;
      line-height: normal;
    }

  }

  .goods-layout {
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow-y: auto;
    max-height: 300px;

    .item-layout {
      display: flex;
      flex-direction: row;
      width: 100%;
      background-color: #FAFAFA;
      padding: 5px;
      margin-bottom: 5px;

      .image {
        width: 80px;
        height: 80px;
      }

      .info-layout {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        flex: 1;

        .name {
          margin-top: 10px;
          color: #333;
          font-weight: 400;
        }

        .spec {
          color: #999;
        }

        .info {
          display: flex;
          flex-direction: row;
          justify-content: space-between;

          .money {
            color: #00afff;
            font-weight: bold;
            font-size: 18px;
          }

          .total {
            color: #333;
            font-weight: bold;
            font-size: 18px;
            margin-right: 20px;
          }
        }
      }
    }
  }

</style>
